package com.ximu.test.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import com.ximu.test.listener.UploadListener;

public class Form extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		response.setContentType("text/html");
//		PrintWriter out = response.getWriter();
//		String name = request.getParameter("name");
//		String passWord = request.getParameter("password");
//		System.out.println(name);
//		System.out.println(passWord);
		try{
			fileUpload(request);
		}catch (FileUploadException e) {
			System.out.println(e.getMessage());
		}
//		request.getRequestDispatcher("/test.jsp").forward(request, response);
//		response.sendRedirect("http://www.baidu.com");
	}
	
	private void fileUpload(HttpServletRequest request)
			throws ServletException,IOException, FileUploadException{
		
		UploadListener pListener = new UploadListener();
		ServletFileUpload upload = new ServletFileUpload();
		upload.setProgressListener(pListener);
		FileItemIterator iter = upload.getItemIterator(request);
		while(iter.hasNext()){
			FileItemStream item = iter.next();
			String name = item.getFieldName();
			InputStream stream = item.openStream();
			if(item.isFormField()){
				System.out.println("Form field " + name + " with value "
			            + Streams.asString(stream) + " detected.");
			}else{
				String baseName = item.getName();
				int site = baseName.lastIndexOf(File.separator);
				if(site>-1){
					baseName = baseName.substring(site);
				}
				System.out.println(baseName);
				File file = new File(this.getServletContext().getRealPath("upload"),baseName);
				file.getParentFile().mkdir();
				file.createNewFile();
				OutputStream ous = new FileOutputStream(file);
				byte[] buffer = new byte[1024];
				int len = 0;
				try{
					while((len = stream.read(buffer)) > -1){
						ous.write(buffer,0,len);
						System.out.println("文件已保存:"+file.getAbsolutePath());
					}				
				}finally{
					ous.close();
					stream.close();
				}
			}
		}
	}
}
